package cn.com.cal;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.common.Poke;
/**
 * 计算牌值
 * @author jj043
 */
public class PokeCal 
{
	private static Logger logger = LoggerFactory.getLogger(PokeCal.class);	
	private int nuType=0;   //牛类型 	
	private int nuValue=0;  //牛值
	private List<Poke> array = null;	
	private int pokeSun=0;
	private int fanBai=1;
	
	
	
	public int getFanBai() 
	{
		if(nuType<=26)
			return 1;
		else if	(nuType<30)
			return 2;
		else if(nuType<40)
			return 3;
		else if(nuType<50)
			return 4;
		else if(nuType<60)
			return 5;
		else if(nuType<70)
			return 7;
		else
			return 7;
	}

	public PokeCal(List<Poke> cardlist)
	{
		this.array = null;
		this.array = cardlist;
		pokeSun=0;
		for(int i=0;i<array.size();i++)
		{
			Poke temp=array.get(i);
			int value=temp.getFlag();
			pokeSun+=value;
		}
	}
	
	/**
	 * 从大到小获得第几张牌
	 * @param k
	 * @return
	 */
	public Poke getCardNum(int k)
	{
		
		int icount=0;
		boolean b=false;
		for(int j=0;j<array.size()-1;j++)
		{
			b=false;
			for(int i=0;i<array.size()-1;i++)
			{
				Poke temp1=array.get(i);
				Poke temp2=array.get(i+1);
				if(temp1.getValue()<temp2.getValue())
				{
					array.set(i,temp2);
					array.set(i+1,temp1);
					b=true;
					}
					icount++;
			}
					if(!b) 
					break;
		}
		Poke temp3=array.get(k);
		return temp3;
	}
	
	public boolean checkNu(int[] cardArray,byte nutype)
	{
		int nu=nutype/10;
		int num=nutype%10;
		switch(nu)
		{
			case 1:
				
				return checkMeiNu(cardArray); 
			case 2:
				return checkYouNu(cardArray,num);
			case 3:
				return checkNuNu(cardArray);
			case 4:
				return checkYangNu(cardArray);
			case 5:
				return checkShiZha(cardArray);
			case 6:
				return checkWuHua(cardArray);
			case 7:
				return checkWuXia(cardArray);
		}
		return false;
	}
	
	//无牛
	private boolean checkMeiNu(int[] cardArray)
	{
		nuType=10;
		return true;
	}
	
	//有牛
	private boolean checkYouNu(int[] cardArray,int num)
	{
		
		int sum=0;
		int nuValue1=0;
		int numValue=0;
		for(int i=0;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int value=cardArray[i]& 0xFF;
			if(i>2)
			{
				if(value>=10)
				{
					numValue+=10;
				}
				else
				{
					numValue+=value;
				}				
			}
			else
			{
				if(value>=10)
				{
					nuValue1+=10;
				}
				else
				{
					nuValue1+=value;
				}
			}
		}
		numValue=numValue%10;
		nuValue=numValue;
		logger.info("原牌："+pokeSun);
		logger.info("现牌："+sum);
		logger.info("值："+numValue);
		if(pokeSun==sum&&nuValue1%10==0&&numValue==num)
		{
			logger.info("验牛成功");
			nuType=20+num;
			return true;
		}
		else
		{
			nuType=10;
			return false;
		}
	}
	
	//牛牛
	private boolean checkNuNu(int[] cardArray)
	{
		
		int sum=0;
		int nuValue=0;
		int numValue=0;
		for(int i=0;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int value=cardArray[i]& 0xFF;
			if(i>2)
			{
				
				if(value>=10)
				{
					numValue+=10;
				}
				else
				{
					numValue+=value;
				}
			}
			else
			{
				if(value>=10)
				{
					nuValue+=10;
				}
				else
				{
					nuValue+=value;
				}
			}
		}
		
		if(pokeSun==sum&&nuValue%10==0&&nuValue%10==0)
		{
			logger.info("验牛成功");
			nuType=30;
			return true;
		}
		{
			nuType=10;
			return false;
		}
	}
	
	
	//银牛
	private boolean checkYangNu(int[] cardArray)
	{
		int sum=0;
		int tenNum=0;
		
		for(int i=0;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int value=cardArray[i]& 0xFF;
			
			if(value<10)
			{
				return false;
			}
			else if(value == 10)
			{
				tenNum++;
			}
			else
				continue;
		}
		
		if(pokeSun==sum&&tenNum==1)
		{
			logger.info("验牛成功");
			nuType=40;
			return true;
		}
		else
		{
			nuType=10;
			return false;
		}
	}
	
	//四炸
	private boolean checkShiZha(int[] cardArray)
	{
		int sum=cardArray[0];
		int sameNum=0;
		int cardValue=cardArray[0]& 0xFF;
		for(int i=1;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int temp=cardArray[i]& 0xFF;
			if(cardValue==temp)
				sameNum++;
		}
		
		if(pokeSun==sum&&sameNum==3)
		{
			logger.info("验牛成功");
			nuType=50;
			return true;
		}
		else
		{
			nuType=10;
			return false;
		}
	}
	
	//五花
	private boolean checkWuHua(int[] cardArray)
	{
		int sum=0;
		int tenNum=0;
		
		for(int i=0;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int value=cardArray[i]& 0xFF;
			nuValue+=value;
			if(value<=10)
			{
				return false;
			}			
			else
				continue;
		}
		
		if(pokeSun==sum)
		{
			logger.info("验牛成功");
			nuType=60;
			return true;
		}
		else
		{
			nuType=10;
			return false;
		}
	}
	
	//五小
	private boolean checkWuXia(int[] cardArray)
	{
		int sum=0;
		int valueSum=0;
		
		for(int i=0;i<cardArray.length;i++)
		{
			sum+=cardArray[i];
			int value=cardArray[i]& 0xFF;
			valueSum+=value;
			if(value>=5)
			{
				return false;
			}			
			else
				continue;
		}
		
		if(pokeSun==sum&&valueSum<=10)
		{
			logger.info("验牛成功");
			nuType=70;
			return true;
		}
		else
		{
			nuType=10;
			return false;
		}
	}
	
	public int getNuType() {
		return nuType;
	}

	public void setNuType(int nuType) {
		this.nuType = nuType;
	}
	

	public int getNuValue() {
		return nuValue;
	}

	public void setNuValue(int nuValue) {
		this.nuValue = nuValue;
	}

}
